package com.xsd.awen.utils;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.Date;

public class ServiceUtils {
    private static String ID = "id";
    private static String CREATE_USER_ID = "createUserId";
    private static String UPDATE_USER_ID = "updateUserId";
    private static String CREATE_TIME = "createTime";
    private static String UPDATE_TIME = "updateTime";
    private static String CUSTOMER_ID = "customerId";
    private static String IS_DEL = "isDel";

    /**
     * only insert
     * @param t
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> T completion(T t, Object entity){
        try {
            Date date = new Date();
            PropertyUtils.setProperty(t ,UPDATE_USER_ID,"-1");
            PropertyUtils.setProperty(t ,CREATE_TIME,date);
            PropertyUtils.setProperty(t ,UPDATE_TIME,date);
            PropertyUtils.setProperty(t,ID, UUIDUtils.generate());
            PropertyUtils.setProperty(t,CUSTOMER_ID, PropertyUtils.getProperty(entity,CUSTOMER_ID));
            PropertyUtils.setProperty(t,CREATE_USER_ID, PropertyUtils.getProperty(entity,CREATE_USER_ID));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static <T> T completion(T t){
        return completion(t,t);
    }

    public static <T> T updateCompletion(T t, Object entity){
        try {
            Object createUserId = PropertyUtils.getProperty(entity,CREATE_USER_ID);
            Object customerId = PropertyUtils.getProperty(entity,CUSTOMER_ID);
            PropertyUtils.setProperty(t ,CREATE_USER_ID,createUserId);
            PropertyUtils.setProperty(t,CUSTOMER_ID,customerId );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static <T> T updateCompletion(T t){
        return updateCompletion(t,t);
    }

}
